#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Last Update: 2010/10/30 11:32:57

'''docstring
'''

__revision__ = '0.1'
__author__ = 'lxd'

import wx
import win32con
import time
import threading
import sys

import mouse
import key
import window
import help_click

class BloodThread(threading.Thread):
    def __init__(self,log,hwnd,m,k):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.log = log
        self.hwnd = hwnd
        self.k = k

    def stop(self):
        self.timeToQuit.set()

    def run(self):
        #while not self.timeToQuit.isSet():
            #self.m.
        pass

class WorkThread(threading.Thread):
    def __init__(self,log):
        threading.Thread.__init__(self)
        self.timeToQuit = threading.Event()
        self.timeToQuit.clear()
        self.log = log
        self.farms = [[87,222],[159,182],[229,142],[165,259],[236,219],[308,179],[242,296],[314,255],[385,216]]
        self.width = 550
        self.height = 400
        #559,245,1109,645

        self.sellThreshold = 18
        self.fruitNum = 0
        
    def stop(self):
        self.timeToQuit.set()

    def checkGame(self):
        width,height= 0,0
        while width != self.width and height != self.height:
            w = window.Window()
            left,top,right,bottom = w.getRect()
            self.log.info('left,top,right,bottom:(%s,%s,%s,%s)' %(left,top,right,bottom))
            width = right - left
            height = bottom - top
            time.sleep(1)
        self.w= w
        return True

    def findEnemy(self):
        '''
        获得敌人坐标
        '''
        e_x0,e_y0 = self.m.findCenterColor(420,20,530,100,'0000CC')#缩略图中敌人
        x0,y0 = 474,59#缩略图中人
        x,y = 284,202#实际人
        if e_x0 != -1 and e_y0 != -1:
            e_x = 5*(e_x0-x0)+x
            e_y = 5*(e_y0-y0)+y
            return e_x,e_y
        else:
            return -1,-1

    def addBlood(self):
        '''
        docstring for addBlood
        '''
        pass
    
    def workAction(self):
        x,y = self.findEnemy()
        self.m.click(x,y)

##########################################################
   
    def work(self):
        self.log.info('move the mouse to the flash game')
        if self.checkGame():
            self.m = mouse.Mouse(self.log,self.w)
            self.k = key.Key(self.log,self.w)
            self.help_click = help_click.Help_click(self.m)
            self.log.info('find game now')
            while not self.timeToQuit.isSet():
                #self.workAction()
                self.testAction()
            else:
                self.log.info('quit')

    def testAction(self):
        self.k.press('1')

    def run(self):
        self.work()

class FrameWithHotKey(wx.Frame):
    def __init__(self, log,*args, **kwargs):
        wx.Frame.__init__(self, *args, **kwargs)
        self.log = log
        self.regHotKey()
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyStart, id=self.hotKeyId_start)
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyEnd, id=self.hotKeyId_end)
        self.Bind(wx.EVT_HOTKEY, self.OnHotKeyQuit, id=self.hotKeyId_quit)
        self.work = None
        self.log.info(u'使用方法：')
        self.log.info(u'Alt+F1:连续点击')
        self.log.info(u'Alt+F2:停止点击')
        self.log.info(u'Alt+F3:关闭')
        
    def regHotKey(self):
        self.hotKeyId_start = 100
        self.RegisterHotKey(self.hotKeyId_start, win32con.MOD_ALT, win32con.VK_F1)
        self.hotKeyId_end = 101
        self.RegisterHotKey(self.hotKeyId_end, win32con.MOD_ALT, win32con.VK_F2)
        self.hotKeyId_quit = 102
        self.RegisterHotKey(self.hotKeyId_quit, win32con.MOD_ALT, win32con.VK_F3)
                
    def OnHotKeyStart(self, evt):
        if not self.work:
            self.work = WorkThread(self.log)
            self.work.setDaemon(True) 
            self.work.start()
            
    def OnHotKeyEnd(self, evt):
        if self.work:
            self.work.stop()
            self.work = None
            
    def OnHotKeyQuit(self, evt):
        sys.exit()
        
if __name__ == '__main__':
    import config
    app = wx.App()
    FrameWithHotKey(config.log,None)
    app.MainLoop()
